package de.gwtprime.client.widgets.notificationCenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.event.AnswerAddedEvent;
import de.gwtprime.client.event.handler.AnswerAddedEventHandler;

public class NotificationCenterWidget extends Composite {

	
	@UiField Label notificationText;
	
	/**
	 * Time to show the notifications before they disappear in milliseconds
	 */
	private int timeToShowNotifictations = 2000;
	
	private static NotificationCenterWidgetUiBinder uiBinder = GWT
			.create(NotificationCenterWidgetUiBinder.class);

	interface NotificationCenterWidgetUiBinder extends
			UiBinder<Widget, NotificationCenterWidget> {
	}

	public NotificationCenterWidget(ClientFactory clientFactory) {
		initWidget(uiBinder.createAndBindUi(this));
		
		addStyleName("notificationCenter notificationHidden");
		
		notificationText.setText("");
		
		clientFactory.getEventBus().addHandler(AnswerAddedEvent.TYPE, new AnswerAddedEventHandler() {
			

			@Override
			public void onAnswerAdded(AnswerAddedEvent event) {
				showMessage("Antwort eingefügt");
			}
		});
		
		
	}
	
	
	/**
	 * Show the given message in the notification center for some seconds
	 * @param message Message to show as notification
	 */
	public void showMessage(String message){
		notificationText.setText(message);
		removeStyleName("notificationHidden");
		 Timer timer = new Timer() {
			    public void run() {
			     addStyleName("notificationHidden");
			     cancel();
			    }
			  };
			  
			  timer.schedule(timeToShowNotifictations);
	}


	public int getTimeToShowNotifictations() {
		return timeToShowNotifictations;
	}

	public void setTimeToShowNotifictations(int timeToShowNotifictations) {
		this.timeToShowNotifictations = timeToShowNotifictations;
	}

}
